// Tags:
#include <algorithm>
#include <cctype>
#include <cstdio>

typedef long long ll;
const int W = 1e6;
ll a, b, c, d, r, lv, ans;

template <typename T>
inline void read(T &x) {
  x = 0;
  bool f = false;
  short ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = true;
    ch = getchar();
  }
  while (isdigit(ch)) x = x * 10 + (ch ^ '0'), ch = getchar();
  if (f) x = -x;
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen64("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen64("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("b.in", "r", stdin);
  freopen("b.out", "w", stdout);
#endif
#endif

  read(a), read(b);
  read(c), read(d);
  r = c / W, lv = W - c % W;
  for (int i = 1; i <= r; ++i) {
    /* while (a >= (r - i + 1) && b >= W * i - lv) {
      ++ans, b -= W * i - lv, a -= r - i + 1, d += W * i - lv;
    } */
    ll k = std::min(a / (r - i + 1), b / (W * i - lv));
    ans += k, b -= k * (W * i - lv), a -= (r - i + 1);
    d += k * (W * i - lv);
  }
  ans += b / c;
  d += (b / c) * c;
  b %= c;
  ll k = std::min(a / (r + 1), d / lv);
  ans += k;
  b += k * lv;
  a -= k * (r + 1);
  ans += b / c;
  d += (b / c) * c;
  b %= c;
  for (int i = 1; i <= r; ++i) {
    ll k = std::min(a / (r - i + 1), b / (W * i - lv));
    ans += k, b -= k * (W * i - lv), a -= (r - i + 1);
  }
  printf("%lld", ans);
  return 0;
}